package com.ysd.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Memeber;
import com.ysd.entity.MemeberSearch;
import com.ysd.repository.MemeberRepository;
import com.ysd.utils.Result;

@RestController
public class MemeberController {

	@Autowired
	private MemeberRepository MemeberRep;

	// 多条件查询
	@RequestMapping("getByWhere")
	public Object getByWhere(MemeberSearch m, Integer page, Integer limit) {
		Specification<Memeber> spec = getMemeSpecification(m);
		Pageable pageable = new PageRequest(page - 1, limit, new Sort(Direction.DESC, "regdate"));
		Page<Memeber> list = MemeberRep.findAll(spec, pageable);
		if (list != null) {
			return Result.toClient("0", "查询成功", (int) list.getTotalElements(), list.getContent());
		} else {
			return Result.toClient("1", "查询失败", null);
		}
	}

	public Specification<Memeber> getMemeSpecification(MemeberSearch m) {
		// java中接口不能被new,以下这种方式叫做"匿名内部类"
		return new Specification<Memeber>() {
			@Override
			public Predicate toPredicate(Root<Memeber> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p = cb.conjunction();
				List<Expression<Boolean>> list = p.getExpressions();

				// root可以理解为实体类,cb对象中封装了所有的数据库操作
				if (m.getCategory() != null) {
					list.add(cb.equal(root.get("category"), m.getCategory()));
				}
				if (m.getDepartment() != null && !m.getDepartment().equals("")) {
					list.add(cb.equal(root.get("department"), m.getDepartment()));
				}
				if (m.getProfession() != null && !m.getProfession().equals("")) {
					list.add(cb.equal(root.get("profession"), m.getProfession()));
				}
				if (m.getGrade() != null && !m.getGrade().equals("")) {
					list.add(cb.equal(root.get("grade"), m.getGrade()));
				}
				if (m.getName() != null && !m.getName().equals("")) {
					list.add(cb.like(root.get("name"), m.getName()));
				}
				if (m.getMemberId() != null) {
					list.add(cb.equal(root.get("memberId"), m.getMemberId()));
				}
				if (m.getPhone() != null && !m.getPhone().equals("")) {
					list.add(cb.equal(root.get("phone"), m.getPhone()));
				}
				if (m.getBeginRegdate() != null) {
					list.add(cb.greaterThanOrEqualTo(root.get("regdate"), m.getBeginRegdate()));
				}
				if (m.getEndRegdate() != null) {
					list.add(cb.lessThanOrEqualTo(root.get("regdate"), m.getEndRegdate()));
				}
				if (m.getStatus() != null) {
					list.add(cb.equal(root.get("status"), m.getStatus()));
				}

				return p;
			}
		};
	}

	// 添加
	@RequestMapping("addMemeber")
	public Object addMemeber(Memeber m) {
		Map<String, Object> map = new HashMap<String, Object>();
		Memeber me = MemeberRep.save(m);
		map.put("code", "0");
		map.put("msg", "添加成功");
		map.put("data", me);
		return map;
	}

	// 根据id删除
	@RequestMapping("delMemeberById")
	public Object delMemeberById(int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			MemeberRep.delete(id);
			map.put("code", "0");
			map.put("msg", "删除成功");
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return map;
		}
	}

	// 根据id修改
	@RequestMapping("updateMemeber")
	public Object updateMemeber(int id, Integer card, Integer category, String department, String grade, String name,
			Integer phone, String profession, String sex) {
		int n = MemeberRep.updateMemeber(id, card, category, department, grade, name, phone, profession, sex);
		Map<String, Object> map = new HashMap<String, Object>();
		if (n > 0) {
			map.put("code", "0");
			map.put("msg", "修改成功");
		} else {
			map.put("code", "1");
			map.put("msg", "修改失败");
		}
		return map;
	}

	// 重置状态
	@RequestMapping("setStatus")
	public Object setMemeberStatus() {
		int n = MemeberRep.setMemeber();
		Map<String, Object> map = new HashMap<String, Object>();
		if (n > 0) {
			map.put("code", "0");
			map.put("msg", "重置成功");
		} else {
			map.put("code", "1");
			map.put("msg", "重置失败");
		}
		return map;
	}

	// 批量删除
	@RequestMapping("delMemeberByIds")
	public Object deletes(String ids) {

		String[] str = ids.split(",");// ids > 1,2,3
		List<Integer> list = new ArrayList<>();
		for (String l : str) {
			Integer i = Integer.valueOf(l);
			list.add(i);
		}
		int n = MemeberRep.deleteMemeberById(list);
		Map<String, Object> map = new HashMap<String, Object>();
		if (n > 0) {
			map.put("code", "0");
			map.put("msg", "删除成功");
		} else {
			map.put("code", "1");
			map.put("msg", "删除失败");
		}
		return map;

	}

}
